package com.example.werthwedemo0309;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;

import com.example.werthwedemo0309.IntentData.IWeatherPresenter;
import com.example.werthwedemo0309.fragment.CityFragment;
import com.example.werthwedemo0309.fragment.PersonFragment;
import com.example.werthwedemo0309.fragment.WeatherFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import static com.example.werthwedemo0309.MainActivity.response;

public class OneActivity extends AppCompatActivity {
    private static final String TAG = OneActivity.class.getSimpleName();
    private BottomNavigationView homeNavi;
    private TextView title_text_city;
    private TextView title_text_wendu;
    private TextView title_text_weather;
    private List<Fragment> mHomeFragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        initView();
        title_text_city.setText(response.body().getResult().getCity());
        title_text_wendu.setText(response.body().getResult().getRealtime().getTemperature());
        title_text_weather.setText(response.body().getResult().getRealtime().getInfo());
    }

    private void initView() {
        title_text_city=findViewById(R.id.title_text_city);
        title_text_wendu=findViewById(R.id.title_text_wendu);
        title_text_weather=findViewById(R.id.title_text_weather);
        homeNavi = findViewById(R.id.home_navi);
        mHomeFragments.add(new CityFragment());
        mHomeFragments.add(new WeatherFragment());
        mHomeFragments.add(new PersonFragment());
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < mHomeFragments.size(); i++)
        {
            ft.add(R.id.home_container, mHomeFragments.get(i));

            if (i != 0)
            {
                ft.hide(mHomeFragments.get(i));
            }
        }
        ft.commit();

        //控制getSupportFragmentManager().beginTransaction().show()/hide()
        homeNavi.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                replaceHomeFragment(item.getItemId());

                return true;
            }
        });
    }
    private void replaceHomeFragment(int id)
    {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        for (int i = 0; i < mHomeFragments.size(); i++)
        {
            ft.hide(mHomeFragments.get(i));
            switch (id)
            {
                case R.id.navi_city:
                    ft.show(mHomeFragments.get(0));
                    break;

                case R.id.navi_weather:
                    ft.show(mHomeFragments.get(1));
                    break;
                case R.id.navi_person:
                    ft.show(mHomeFragments.get(2));
                    break;
            }
        }
        ft.commit();
    }

}